package com.hilaver.dzmis.web.servlet.export;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.service.impl.PDFExportServiceImpl;
import com.hilaver.dzmis.service.impl.PDFExportServiceImpl1;
import com.hilaver.dzmis.service.impl.PDFMeasureSpecificationExportServiceImpl;
import com.hilaver.dzmis.service.impl.PDFOrderStatisticsExportServiceImpl;
import com.hilaver.dzmis.util.PDFPageXOfY;
import com.hilaver.dzmis.vo.OrderStatisticsCriteria;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;

/**
 * Servlet implementation class for Servlet: PDFExportServlet
 * 
 */
public class PDFExportServlet extends AbstractBaseServlet {
	static final long serialVersionUID = 1L;

	public final static int ACTION_PRODUCT_IDENTIFICATION_EXPORT = 1;
	
	public final static int ACTION_ORDER_STATISTICS_EXPORT = 2;
	
	public final static int ACTION_MEASURE_SPECIFICATION_EXPORT = 3;

	public PDFExportServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer id = null;
		int action = Integer.parseInt(request.getParameter("action"));
		String input = null;
		
		String pdfName;
		
		if(ACTION_PRODUCT_IDENTIFICATION_EXPORT == action){
			id = Integer.parseInt(request.getParameter("id"));
			input = request.getParameter("input");
			pdfName = PDFExportServiceImpl.pdfFileName(id);
		}else if(ACTION_MEASURE_SPECIFICATION_EXPORT == action){
			id = Integer.parseInt(request.getParameter("id"));
			pdfName = PDFMeasureSpecificationExportServiceImpl.pdfFileName(id);
		}else if(ACTION_ORDER_STATISTICS_EXPORT == action){
			try {
				this.fillObj(OrderStatisticsCriteria.class.getName(), request);
			} catch (Exception e) {
			}
			pdfName = PDFOrderStatisticsExportServiceImpl.getTableTitle((OrderStatisticsCriteria)this.obj, (Locale) request.getSession()
						.getAttribute(Constants.SESSION_LOCALE));
		}else{
			pdfName = "error";
		}
		
		response.setHeader("Expires", "0");
		response.setHeader("Cache-Control",
				"must-revalidate, post-check=0, pre-check=0");
		response.setHeader("Pragma", "public");
		response.setContentType(Constants.PDF_RESPONSE_TYPE);
		response.setHeader("Content-disposition",
                "attachment; filename=" + pdfName +
                ".pdf" );
		

		Document document = new Document();
		try {
			PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());

			switch (action) {
			case ACTION_PRODUCT_IDENTIFICATION_EXPORT:
				PDFExportServiceImpl1 pdfExporter = new PDFExportServiceImpl1(document, (Locale) request.getSession()
						.getAttribute(Constants.SESSION_LOCALE));
				pdfExporter.setInput(input);
				pdfExporter.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
				writer.setPageEvent(new PDFPageXOfY());
				document.open();
				
				pdfExporter.exportProductionIdentification(id);
				break;
			case ACTION_ORDER_STATISTICS_EXPORT:
				this.fillObj(OrderStatisticsCriteria.class.getName(), request);
				
				PDFOrderStatisticsExportServiceImpl osExport = new PDFOrderStatisticsExportServiceImpl(document, (Locale) request.getSession()
						.getAttribute(Constants.SESSION_LOCALE));
				writer.setPageEvent(new PDFPageXOfY());
				document.open();
				osExport.exportOrderStatistics((OrderStatisticsCriteria)this.obj);
				break;
			case ACTION_MEASURE_SPECIFICATION_EXPORT:
				PDFMeasureSpecificationExportServiceImpl msExport = new PDFMeasureSpecificationExportServiceImpl(document, (Locale) request.getSession()
						.getAttribute(Constants.SESSION_LOCALE));
				msExport.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
				writer.setPageEvent(new PDFPageXOfY());
				document.open();
				msExport.exportMeasureSpecification(id);	
				break;
			}
		} catch (Exception de) {
			de.printStackTrace();
			try {
				document.add(new Phrase(de.getMessage()));
			} catch (DocumentException e) {
			}
		}

		document.close();

	}
}